/*********************************************************************************************************
**
**                                   GJB 标准测试集
**
**                               Copyright All Rights Reserved
**
**--------------文件信息--------------------------------------------------------------------------------
**
** 文   件   名: gjb_S0102603AQ.c
**
** 文件创建日期: 2021 年 1 月 20 日
**
** 描        述: 国军标函数接口长期稳定性测试, 通过调用不同的函数接口并判断函数的返回值
*********************************************************************************************************/
#define _XOPEN_SOURCE
#include <pthread.h>
#include <signal.h>
#include <sys/types.h>
#include <errno.h>
#include <semaphore.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <fcntl.h>


#include <stdlib.h>

#include <unistd.h>


#include <limits.h>
#include <string.h>

#include <bits/sigevent-consts.h>


#ifdef SYLIXOS
#define __SYLIXOS_KERNEL
#endif

// #include "gjb.h"

#define TimeLen         1800
#define DelayTime       30

#define TIMER_RELATIVE_C 0

# define CLOCK_REALTIME			0

// mpart_create增加
typedef int mpart_id;
mpart_id MPARTID = 0;
#define MAX_PART 256
#define EMNOTINITED 0x7A
#define BASE_MID 300

static pthread_t       thread_id1;
static pthread_t       thread_id2;
static pthread_t       thread_id3;
static pthread_t       thread_id4;
static pthread_t       thread_id5;
static pthread_t       thread_id6;
static pthread_t       thread_id7;
static pthread_t       thread_id8;
static pthread_t       thread_id9;
static pthread_t       thread_id10;

static pthread_attr_t  thread_attr1;
static pthread_attr_t  thread_attr2;
static pthread_attr_t  thread_attr3;
static pthread_attr_t  thread_attr4;
static pthread_attr_t  thread_attr5;
static pthread_attr_t  thread_attr6;
static pthread_attr_t  thread_attr7;
static pthread_attr_t  thread_attr8;
static pthread_attr_t  thread_attr9;
static pthread_attr_t  thread_attr10;

static pthread_mutex_t Amutex;
static sem_t           Bsem;

static int             bFlag = 0;

static void *Fun1_security(void *arg);
static void *Fun2_security(void *arg);
static void *Fun3_security(void *arg);
static void *Fun4_security(void *arg);
static void *Fun5_security(void *arg);
static void *Fun6_security(void *arg);
void *Fun7_security(void *arg);
void *Fun8_security(void *arg);
void *Fun9_security(void *arg);
void *Fun10_security(void *arg);
int S0102103AQ(void);
int mpart_create(char *addr,size_t size,mpart_id *mid);
int pthread_delay(int ticks);
int mpart_delete(mpart_id mid);
int mpart_free(mpart_id mid,char* addr);
void* mpart_alloc(mpart_id mid,size_t size);



void handler_xx (int signo)
{
    printf("Fun handler is running\n");
	bFlag = 1;
}

int Test_S0102103AQ (void)
{
	int             res = 0;
	clockid_t       clock_id = CLOCK_REALTIME;
	struct timespec tp;
	struct timespec rqtp;
	long            nTime1, nTime2;
	int             nRunTimes=0;
	
	pthread_mutex_init(&Amutex, NULL);

	sem_init(&Bsem, 0, 2);
	
	if (res == 0) {
		res = clock_gettime(clock_id, &tp);
		if (res == 0) {
			nTime1 = tp.tv_sec;
			printf("nTime1 = %ld\n", nTime1);

		} else {
		    printf("clock_gettime nTime1 error\n");
			return (-1);
		}

	} else {
	    printf("clock_getcpuclockid error\n");
		return  (-1);
	}

	while (1) {
		if (nRunTimes++ % 50 == 0) {
		    printf("Rounds:%d\n", nRunTimes);
			// mem_show();
			system("cat /proc/meminfo");
		}

		S0102103AQ();

		clock_gettime(clock_id, &tp);

		nTime2 = tp.tv_sec;

		printf("Time:\tnTime1 = %ld\tnTime2 = %ld\tnTime2 - nTime1 = %ld\n", nTime1, nTime2, nTime2 - nTime1);

		if (nTime2 - nTime1 > TimeLen) {
			pthread_delay(1000);
			printf("Time Over! \t nTime1 = %ld \t nTime2 = %ld \t nTime2 - nTime1 = %ld\n", nTime1, nTime2, nTime2 - nTime1);
			break;

		} else {
			rqtp.tv_sec = DelayTime;
			rqtp.tv_nsec = 0;
			nanosleep(&rqtp, NULL);
		}
	}

	return  (0);
}

int S0102103AQ (void)
{
	int res = -100;

	pthread_attr_init(&thread_attr1);
	pthread_attr_init(&thread_attr2);
	pthread_attr_init(&thread_attr3);
	pthread_attr_init(&thread_attr4);
	pthread_attr_init(&thread_attr5);
	pthread_attr_init(&thread_attr6);
	pthread_attr_init(&thread_attr7);
	pthread_attr_init(&thread_attr8);
	pthread_attr_init(&thread_attr9);
	pthread_attr_init(&thread_attr10);

	// thread_attr1.stackaddr = NULL;
	// thread_attr1.stacksize = PTHREAD_STACK_MIN*2;
	// thread_attr1.inheritsched = PTHREAD_EXPLICIT_SCHED;
	// thread_attr1.schedpolicy = SCHED_FIFO;
	// thread_attr1.schedparam.sched_priority = 90;
	// thread_attr1.name = "Task1";

	// thread_attr2.stackaddr = NULL;
	// thread_attr2.stacksize = PTHREAD_STACK_MIN*2;
	// thread_attr2.inheritsched = PTHREAD_EXPLICIT_SCHED;
	// thread_attr2.schedpolicy = SCHED_FIFO;
	// thread_attr2.schedparam.sched_priority = 90;
	// thread_attr2.name = "Task2";

	// thread_attr3.stackaddr = NULL;
	// thread_attr3.stacksize = PTHREAD_STACK_MIN*2;
	// thread_attr3.inheritsched = PTHREAD_EXPLICIT_SCHED;
	// thread_attr3.schedpolicy = SCHED_FIFO;
	// thread_attr3.schedparam.sched_priority = 90;
	// thread_attr3.name = "Task3";

	// thread_attr4.stackaddr = NULL;
	// thread_attr4.stacksize = PTHREAD_STACK_MIN*2;
	// thread_attr4.inheritsched = PTHREAD_EXPLICIT_SCHED;
	// thread_attr4.schedpolicy = SCHED_FIFO;
	// thread_attr4.schedparam.sched_priority = 100;
	// thread_attr4.name = "Task4";

	// thread_attr5.stackaddr = NULL;
	// thread_attr5.stacksize = PTHREAD_STACK_MIN*2;
	// thread_attr5.inheritsched = PTHREAD_EXPLICIT_SCHED;
	// thread_attr5.schedpolicy = SCHED_FIFO;
	// thread_attr5.schedparam.sched_priority = 100;
	// thread_attr5.name = "Task5";

	// thread_attr6.stackaddr = NULL;
	// thread_attr6.stacksize = PTHREAD_STACK_MIN*2;
	// thread_attr6.inheritsched = PTHREAD_EXPLICIT_SCHED;
	// thread_attr6.schedpolicy = SCHED_FIFO;
	// thread_attr6.schedparam.sched_priority = 100;
	// thread_attr6.name = "Task6";

	// thread_attr7.stackaddr = NULL;
	// thread_attr7.stacksize = PTHREAD_STACK_MIN*2;
	// thread_attr7.inheritsched = PTHREAD_EXPLICIT_SCHED;
	// thread_attr7.schedpolicy = SCHED_FIFO;
	// thread_attr7.schedparam.sched_priority = 110;
	// thread_attr7.name = "Task7";

	// thread_attr8.stackaddr = NULL;
	// thread_attr8.stacksize = PTHREAD_STACK_MIN*2;
	// thread_attr8.inheritsched = PTHREAD_EXPLICIT_SCHED;
	// thread_attr8.schedpolicy = SCHED_FIFO;
	// thread_attr8.schedparam.sched_priority = 110;
	// thread_attr8.name = "Task8";

	// thread_attr9.stackaddr = NULL;
	// thread_attr9.stacksize = PTHREAD_STACK_MIN*2;
	// thread_attr9.inheritsched = PTHREAD_EXPLICIT_SCHED;
	// thread_attr9.schedpolicy = SCHED_FIFO;
	// thread_attr9.schedparam.sched_priority = 110;
	// thread_attr9.name = "Task9";

	// thread_attr10.stackaddr = NULL;
	// thread_attr10.stacksize = PTHREAD_STACK_MIN*2;
	// thread_attr10.inheritsched = PTHREAD_EXPLICIT_SCHED;
	// thread_attr10.schedpolicy = SCHED_FIFO;
	// thread_attr10.schedparam.sched_priority = 120;
	// thread_attr10.name = "Task10";

	res = -100;
	res = pthread_create( &thread_id1,&thread_attr1, Fun1_security, NULL );
	if (res == 0) {
	    printf( "Task1 create succesful\n");

	} else {
	    printf( "Task1 create Failed\n");
        goto error;
	}
	
	res = -100;
	res = pthread_create( &thread_id2,&thread_attr2, Fun2_security, NULL );
	if (res == 0) {
	    printf( "Task2 create succesful\n");

	} else {
	    printf( "Task2 create Failed\n");
        goto error;
	}
	
	res = -100;
	res = pthread_create( &thread_id3,&thread_attr3, Fun3_security, NULL );
	if (res == 0) {
	    printf( "Task3 create succesful\n");

	} else {
	    printf( "Task3 create Failed\n");
        goto error;
	}
	
	res = -100;
	res = pthread_create( &thread_id4,&thread_attr4, Fun4_security, NULL );
	if (res == 0) {
	    printf( "Task4 create succesful\n");
	} else {
	    printf( "Task4 create Failed\n");
        goto error;
	}
	
	res = -100;
	res = pthread_create( &thread_id5,&thread_attr5, Fun5_security, NULL );
	if (res == 0) {
	    printf( "Task5 create succesful\n");

	} else {
	    printf( "Task5 create Failed\n");
        goto error;
	}
	
	res = -100;
	res = pthread_create(&thread_id6,&thread_attr6, Fun6_security, NULL);
	if (res == 0) {
		printf( "Task6 create succesful\n");

	} else {
	    printf( "Task6 create Failed\n");
        goto error;
	}
	
	res = -100;
	res = pthread_create(&thread_id7,&thread_attr7, Fun7_security, NULL);
	if (res == 0) {
		printf( "Task7 create succesful\n");

	} else {
	    printf( "Task7 create Failed\n");
        goto error;
	}
	
	res = -100;
	res = pthread_create(&thread_id8,&thread_attr8, Fun8_security, NULL);
	if (res == 0) {
		printf( "Task8 create succesful\n");

	} else {
	    printf( "Task8 create Failed\n");
        goto error;
	}
	
	res = -100;
	res = pthread_create( &thread_id9,&thread_attr9, Fun9_security, NULL);
	if (res == 0) {
		printf( "Task9 create succesful\n");

	} else {
	    printf( "Task9 create Failed\n");
	    goto error;
	}
	
	res = -100;
	res = pthread_create(&thread_id10,&thread_attr10, Fun10_security, NULL);
	if (res == 0) {
	    printf( "Task10 create succesful\n");

	} else {
	    printf( "Task10 create Failed\n");
	    goto error;
	}

	pthread_join(thread_id1, NULL);
	pthread_join(thread_id2, NULL);
	pthread_join(thread_id3, NULL);
	pthread_join(thread_id4, NULL);
	pthread_join(thread_id5, NULL);
	pthread_join(thread_id6, NULL);
	pthread_join(thread_id7, NULL);
	pthread_join(thread_id8, NULL);
	pthread_join(thread_id9, NULL);
	pthread_join(thread_id10, NULL);

	return  (0);

error:
    return  (-1);
}

void *Fun1_security (void *arg)
{
	int res = -100;
	int nDelaytime = 20;
	
	res = pthread_mutex_lock(&Amutex);
	if (res != 0) {
	    printf("Task1:Get Amutex Failed\n");
        return ((void *)123);
	}
	
	pthread_delay(nDelaytime);//1000??ticks
	
	res = -100;
	res = pthread_mutex_unlock(&Amutex);
	
	if (res != 0) {
	    printf("Task1:Release Amutex Failed\tres = %d \n", res);
        return ((void *)123);
	}

	return NULL;
}

void *Fun2_security (void *arg)
{
	int res = -100;
	int nDelaytime = 20;
	
	res = pthread_mutex_lock(&Amutex);
	if (res != 0) {
	    printf("Task2:Get Amutex Failed\n");
        return ((void *)123);
	}
	
	pthread_delay(nDelaytime);
	
	res = -100;
	res = pthread_mutex_unlock(&Amutex);
	
	if (res != 0) {
	    printf("Task2:Release Amutex Failed\n");
        return ((void *)123);
	}

	return NULL;
}

void *Fun3_security (void *arg)
{
	int res = -100;
	int nDelaytime = 20;
	
	res = sem_wait(&Bsem);
	if (res != 0) {
	    printf("Task3:Get Bsem Failed\n");
        return ((void *)123);
	}

	pthread_delay(nDelaytime);
	
	res = sem_post(&Bsem);
	if (res != 0) {
	    printf("Task3:Release Bsem Failed\n");
        return ((void *)123);
	}

	return NULL;
}

void *Fun4_security (void *arg)
{
	int res = -100;
	int nDelaytime = 20;
	
	res = pthread_mutex_lock(&Amutex);
	if (res !=0 ) {
	    printf("Task4:Get Amutex Failed\n");
        return ((void *)123);
	}
	
	pthread_delay(nDelaytime);
	
	res = -100;
	res = pthread_mutex_unlock(&Amutex);
	if (res != 0) {
	    printf("Task4:Release Amutex Failed\n");
        return ((void *)123);
	}

	return NULL;
}

void *Fun5_security (void *arg)
{
	int res = -100;
	int nDelaytime = 20;
	
	res = sem_wait(&Bsem);
	if (res != 0) {
	    printf("Task5:Get Bsem Failed\n");
        return ((void *)123);
	}

	pthread_delay(nDelaytime);
	
	res = sem_post(&Bsem);
	if (res != 0) {
	    printf("Task5:Release Bsem Failed\n");
        return ((void *)123);
	}

	return  NULL;
}

void *Fun6_security (void *arg)
{
	int res = -100;
	int nDelaytime = 20;
	
	res = sem_wait(&Bsem);
	if (res != 0) {
	    printf("Task6:Get Bsem Failed\n");
        return ((void *)123);
	}

	pthread_delay(nDelaytime);
	
	res = sem_post(&Bsem);
	if (res != 0) {
	    printf("Task6:Release Bsem Failed\n");
        return ((void *)123);
	}

	return  NULL;
}

void *Fun7_security (void *arg)
{
	int     res = -100;
	int     nDelaytime = 20;
	
	res = sem_wait(&Bsem);
	if (res != 0) {
	    printf("Task7:Get Bsem Failed\n");
        return ((void *)123);
	}

	pthread_delay(nDelaytime);
	
	res = sem_post(&Bsem);
	if (res != 0) {
	    printf("Task7:Release Bsem Failed\n");
        return ((void *)123);
	}

	return NULL;
}
// zijitianjia
struct itimerspec
{
 struct timespec it_interval; //定时器周期值
 struct timespec it_value; //定时器到期值
};

union sigval { 
       int     sival_int;
       void   *sival_ptr;
   };

struct sigevent {
       int          sigev_notify; /* 通知方式 */
       int          sigev_signo;  /* 通知信号 */
       union sigval sigev_value;  /* 伴随通知所传递的数据 */
       void       (*sigev_notify_function) (union sigval);
                        /* 通知方式设置为"线程通知" (SIGEV_THREAD)时新线程的线程函数 */
       void        *sigev_notify_attributes;
                        /* 通知方式设置为"线程通知" (SIGEV_THREAD)时新线程的属性 */
       pid_t        sigev_notify_thread_id;
                        /* 通知方式为SIGEV_THREAD_ID时，接收信号的线程的pid */
   };

   

void *Fun8_security (void *arg)
{
	int                 res = -100;
	timer_t             tid;
    struct itimerspec   ovalue;
    struct itimerspec   its;
	struct sigevent     ev;
	struct sigaction    act;

	ev.sigev_notify = SIGEV_SIGNAL;
	ev.sigev_signo = SIGALRM;

	act.sa_handler = handler_xx;
	act.sa_flags=0;

	if (sigemptyset(&act.sa_mask) != 0) {
	    printf("sigemptyset() was not successful\n");
        return ((void *)123);
	}

	if (sigaction(SIGALRM, &act, 0) != 0) {
	    printf("sigaction() was not successful\n");
        return ((void *)123);
	}

	bFlag = 0;

	res = timer_create(CLOCK_REALTIME, &ev, &tid);
	if (res != 0) {
	    printf("timer_create Failed\n");
        return ((void *)123);
	}

	its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0;
	its.it_value.tv_sec = 1; its.it_value.tv_nsec = 0;

	res = timer_settime(tid, TIMER_RELATIVE_C, &its, &ovalue);
	if (res != 0) {
	    printf("flag = TIMER_RELACTIVE_C, timer_settime Failed\n");
        return ((void *)123);
	}

	sleep(2);

	if (bFlag != 1) {
	    printf("Task8:DSQ Operate Error\n");
        return ((void *)123);
	}

	res = timer_delete(tid);
	if (res != 0) {
	    printf("timer_delete Failed\n");
        return ((void *)123);
	}

	return NULL;
}

void *Fun9_security (void *arg)
{
#ifdef __TMS320C6X__
#define  MALLOC_NUM   (1)
#define  MALLOC_ONCE_SIZE   (1024 * 1024)
#else
#define  MALLOC_NUM   (10)
#define  MALLOC_ONCE_SIZE   (10 * 1024 * 1024)
#endif
	int         nDelaytime = 20;
	int         i = 0;
	char       *nfp[MALLOC_NUM] = {NULL};
	int    mid;
	int         res;
	
	for (i = 0; i < MALLOC_NUM; i++) {
		nfp[i] = (char*)malloc(1024 * (i + 1));
		if (!nfp[i]) {
		    printf("Task9:malloc Failed\t i = %d\n", i);
	        return ((void *)123);
		}
	}
	
	pthread_delay(2 * nDelaytime);

	for (i = 0; i < MALLOC_NUM; i++) {
		if (nfp[i]) {
			free(nfp[i]);
		}
	}

	char *acoinfoaddr = NULL;
	acoinfoaddr = (char *)malloc(MALLOC_ONCE_SIZE);
	if (acoinfoaddr == NULL) {
	    printf("malloc error errno: %d!\n", errno);
	    return ((void *)123);
	}

	res = mpart_create(acoinfoaddr, MALLOC_ONCE_SIZE, &mid);
	if (res != 0) {
	    perror("mpart_create");
	    printf("Task9:mpart_create Failed %d\n", res);
        return ((void *)123);
	}

	for (i = 0; i < MALLOC_NUM; i++) {
		nfp[i] = (char*)mpart_alloc(mid, 1024 * (i + 1));
		if (!nfp[i]) {
		    printf("Task9:mpart_alloc Failed\t i = %d\n", i);
	        return ((void *)123);
		}
	}

	pthread_delay(nDelaytime);

	for (i = 0; i < MALLOC_NUM; i++) {
		if (nfp[i]) {
			res = mpart_free(mid, nfp[i]);
			if (res != 0) {
			    printf("Task9: Failed Release\n");
		        return ((void *)123);
			}
		}
	}

	res = mpart_delete(mid);
	if (res != 0) {
	    printf("Task9:mpart_delete Failed\n");
        return ((void *)123);
	}

	free(acoinfoaddr);

	return NULL;
}

void *Fun10_security (void *arg)
{
	int     res = -100;
	int     nDelaytime = 3;
	char    ctr1[50] = {0};
	char    ctr2[50] = {0};
	char    cname[50] = {0};
	int     findle = 0;
	int     i = 0;
	int     nflag = 0;
	static int ncount = 0;

	sprintf(cname, "%s%d%s", "abc", (ncount++) % 20, ".txt");

	if (ncount < 21) {
		res = creat(cname, 0666);
		if (res != -1) {
		    close(res);

		} else {
		    printf("Task10: File create Failed\tres = %d\n", res);
		    printf("errno = %d\n", errno);
	        return ((void *)123);
		}
	}

	for (i = 0; i < 50; i++) {
		if (i % 2 == 0) {
			ctr1[i] = 'A';
		} else {
			ctr1[i] = '5';
		}
	}

	ctr1[49] = 0;
	findle = open(cname, O_RDWR, 0666);
	if (findle == -1) {
	    printf("Write, Open File Failed\n");
	    printf("errno = %d\n", errno);
	    printf("ncount = %d\n", ncount);
        return ((void *)123);
	}

	res = write(findle, ctr1, 50);
	if (res == -1) {
	    printf("Write File Failed\n");
	    printf("errno = %d\n", errno);
		close(findle);
        return ((void *)123);
	}

	pthread_delay(10 * nDelaytime);

	res = close(findle);
	if (res != 0) {
	    printf("Close File Failed\n");
        return ((void *)123);
	}

	findle = open(cname, O_RDWR, 0666);
	if (findle == -1) {
	    printf("Read, Open File Failed\n");
        return ((void *)123);
	}

	res = read(findle, ctr2, 50);
	if (res == -1) {
	    printf("Read File Failed\n");
        return ((void *)123);
	}

	nflag = 0;

	for (i = 0; i < 50; i++) {
		if (ctr1[i] != ctr2[i]) {
			nflag = 1;
			printf("ctr1 = %c\tctr2 = %c\n", ctr1[i], ctr2[i]);
			break;
		}
	}

	if (nflag == 1) {
	    printf("Read Data Failed\n");
        return ((void *)123);
	}

	res = close(findle);
	if (res != 0) {
	    printf("Close File Failed\n");
        return ((void *)123);
	}

	return NULL;
}
/**********************************************************************
 * 函数名称： GJB_ENTRY()
 * 功能描述： 用户程序入口
 * 输入参数： 无
 * 输出参数： 无
 * 返 回 值： 无
 * 其它说明： 无
 **************************************************************************/
int main(int argc, char **argv)
{
    int  result;

    result = Test_S0102103AQ();           /* security         */
    if (result != 0) {
        return (-1);
    }

    return (0);
}
typedef int mpart_id;
// mpart_id MPARTID = 0;
#define MAX_PART 256
#define EMNOTINITED 0x7A
#define BASE_MID 300
#define SLICE_MAX 10
typedef unsigned int uint;
mpart_id MPARTID;
struct mc_node {
    mpart_id        mid;
    struct mc_node *next       ;
} Node;
typedef struct __offset_part
{
    char * addr;
    char isinuse;
    size_t len;
}offset_part;
typedef struct _tag_addr_mpart
{
    char* mpart;
    size_t size;
    size_t free;
    int used;
    uint offset_index;
    offset_part offset[SLICE_MAX];
}addr_mpart;

addr_mpart mpart_global[MAX_PART];
int mpart_create(char *addr,size_t size,mpart_id *mid)
{
    if(addr == NULL ||mid ==NULL || size ==(size_t)-1)
    {
        errno=EINVAL;
        return -1;
    }
    if(MPARTID>MAX_PART-1 || size==0x7fffffff)
    {
        errno=EAGAIN;
        return -1;
    }
    if(MPARTID == -1)
    {
        errno=EMNOTINITED;
        return -1;
    }
    mpart_global[MPARTID].mpart=addr;
    mpart_global[MPARTID].size=size;
    mpart_global[MPARTID].free=size;
    mpart_global[MPARTID].used=0;
    *mid=BASE_MID+MPARTID++;

    return 0;
}

typedef unsigned long long u64;
typedef	unsigned int 	u32;
#define SYSTEM_TICKS_USEC        1000
#define EMINITED 305
u64 g_pthread_delay_tick = 0;
int is_set_sys_rate = 0;
u32 g_sys_uleep_tick = 0;
int pthread_delay(int ticks)
{
    g_pthread_delay_tick = ticks;
    sched_yield();
    if(is_set_sys_rate)
    {
        return usleep(g_sys_uleep_tick * ticks);
    }
    else
    {
        return usleep(SYSTEM_TICKS_USEC * ticks);
    }
}

int mpart_delete(mpart_id mid){
    int i=0;
    if(mid==0)
    {
        errno= EINVAL;
        return -1;
    }
    if(mid<BASE_MID || mid > (BASE_MID+MPARTID))
    {
        errno=EINVAL;
        return -1;
    }
    mid=mid-BASE_MID;

    if(mpart_global[mid].used==-1)
    {
        return -1;
    }
    mpart_global[mid].offset_index=0;
    mpart_global[mid].free= mpart_global[mid].size;
    mpart_global[mid].used=-1;
    memset(mpart_global[mid].offset,0,SLICE_MAX*sizeof(offset_part));
    return 0;
}

int mpart_free(mpart_id mid,char* addr)
{
    int ret = 0;
    int i = 0;
    if(mid<BASE_MID || mid > (BASE_MID+MPARTID))
    {
        errno=EINVAL;
        return -1;
    }
    mid=mid-BASE_MID;
    if(addr == NULL||mpart_global[mid].mpart ==NULL){
        errno= EINVAL;
        return -1;
    }
    if(MPARTID == -1)
    {
        errno=EMINITED;
        return -1;
    }addr_mpart mpart_global[MAX_PART];
    for(i=0;i<SLICE_MAX;i++)
    {

        if(mpart_global[mid].offset[i].addr==addr)
        {
            if(mpart_global[mid].offset[i].isinuse==0)
            {
                ret =-1;
                errno=EMINITED;
                break;
            }
            mpart_global[mid].offset[i].isinuse=0;
            mpart_global[mid].free += 48;
            mpart_global[mid].free += mpart_global[mid].offset[i].len;
            ret =0;
            break;
        }
    }
    return ret;
}

void* mpart_alloc(mpart_id mid,size_t size)
{
    // printf("MPARTID=%x,mpart=%x\n",MPARTID,mpart_global[mid].mpart);
    if(mid<BASE_MID || mid > (BASE_MID+MPARTID))
    {
        errno=EINVAL;
        return NULL;
    }
    mid=mid-BASE_MID;
    if (size >= 0x7fffffff)
    {   
        errno = ENOMEM;
        return NULL;
    }
    if( size<=0 ||size == (size_t)-1 )
    {
        errno=EINVAL;
        return NULL;
    }
    if(size>mpart_global[mid].free)
    {
        errno=ENOMEM;
        return NULL;
    }
    if(MPARTID == -1)
    {
        errno=0x7A;
        return NULL;
    }

    mpart_global[mid].free -= size;
    char *ptr;

    ptr= mpart_global[mid].mpart+mpart_global[mid].used;
    mpart_global[mid].used +=size;
    int index=mpart_global[mid].offset_index;
    mpart_global[mid].offset[index].addr=ptr;
    mpart_global[mid].offset[index].isinuse=1;
    mpart_global[mid].offset[index].len=size;
    mpart_global[mid].offset_index++;
    return ptr;

}
